package com.bulletelc.apollopre.module.main.classify;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.entity.Classify;

import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/13
 * desc  : 一级分类的adapter
 */

public class ClassifyFirstAdapter extends BaseAdapter {
    private Context context;
    private List<Classify> classifyList;
    private LayoutInflater inflater;
    private int selected=0;

    public ClassifyFirstAdapter(Context context, List<Classify> classifyList) {
        this.context = context;
        this.classifyList = classifyList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return classifyList!=null?classifyList.size():0;
    }

    @Override
    public Object getItem(int position) {
        return classifyList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.classify_first_item,parent,false);
            holder = new ViewHolder();
            holder.tv = convertView.findViewById(R.id.tvClassifyFirst);
            holder.vBar = convertView.findViewById(R.id.vBar);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv.setText(classifyList.get(position).getName());
        if (selected == position) {
            holder.tv.setTextColor(context.getResources().getColor(R.color.text_red));
            holder.vBar.setVisibility(View.VISIBLE);
        } else {
            holder.tv.setTextColor(context.getResources().getColor(R.color.black));
            holder.vBar.setVisibility(View.GONE);
        }

        return convertView;
    }

    public void changeItemSelected(int position) {
        if (position != selected) {
            selected=position;
            notifyDataSetChanged();
        }

    }

    class ViewHolder{
        TextView tv;
        View vBar;
    }
}
